<?php
/**
 * 	Bushido
 *
 * @category   Bushido
 * @package    Bushido_Utils
 * @copyright  Copyright (c) 2008-2009 Nathan Keyes
 * @author Nathan Keyes
 * @version    $Id: Utils.php 7 2009-11-14 22:42:08Z nkeyes $
 */

/**
 * Class for common utility funnctions
 */
 class Bushido_Utils
 {
 	/**
 	 * Adds a SimpleXMLElement as a child of another SimpleXMLElement
 	 * 
 	 * @return void
 	 * @param SimpleXMLElement $parent the parent node
 	 * @param SimpleXMLElement $new_child child node to add
 	 */
	public static function simplexml_append(SimpleXMLElement $parent, SimpleXMLElement $new_child)
	{
		$node1 = dom_import_simplexml($parent);
		$dom_sxe = dom_import_simplexml($new_child);
		$node2 = $node1->ownerDocument->importNode($dom_sxe, true);
		$node1->appendChild($node2);
	}
	
	/**
	 * Removes a SimpleXMLElement as a child of another SimpleXMLElement
	 * @return void
	 * @param SimpleXMLElement $parentSXE the parent node
	 * @param SimpleXMLElement $oldSXE child node to remove
	 */
	public static function simplexml_remove(SimpleXMLElement $parentSXE, SimpleXMLElement $oldSXE)
	{
		$parentNode = dom_import_simplexml($parentSXE);
		$oldNode = dom_import_simplexml($oldSXE);
		$parentNode->removeChild($oldNode);
	}
	
	/**
	 * Replaces a child SimpleXMLElement node with another SimpleXMLElement node of a given parent SimpleXMLElement node
	 * @return void
	 * @param SimpleXMLElement $parentSXE the parent node
	 * @param SimpleXMLElement $newSXE new node to be added
	 * @param SimpleXMLElement $oldSXE old node to be replaced
	 */
	public static function simplexml_replace_child(SimpleXMLElement $parentSXE, SimpleXMLElement $newSXE, SimpleXMLElement $oldSXE)
	{
		self::simplexml_remove($parentSXE, $oldSXE);
		self::simplexml_append($parentSXE, $newSXE);
	}
	
	/**
	 * Converts hex values to ASCII
	 *  
	 * @param string $hex the hex value to convert
	 * @return string
	 */
	public static function hex2ASCII($hex)
	{
	    $ASCII = '';
	
	    for($i=0; $i<strlen($hex); $i += 2)
	    {
	        $ASCII .= chr(hexdec(substr($hex, $i, 2)));
	    }
	    return $ASCII;
	}
	
	/**
	 * 
	 * @param $string
	 * @return string
	 */
	public static function JSSafe($string)
	{
		return preg_replace('/([\n\'])/', '\\\$1', $string);
	}
	
	
	/**
	 * 
	 * @param $x
	 * @param $y
	 * @return array
	 */
	public static function linear_regression($x, $y)
	{
	
		// calculate number points
		$n = count($x);
		
		// ensure both arrays of points are the same size
		if ($n != count($y))
		{
			trigger_error("linear_regression(): Number of elements in coordinate arrays do not match.", E_USER_ERROR);
		}
		if($n == 0)
		{
			return array("m"=>0, "b"=>0);
		}
		
		// calculate sums
		$x_sum = array_sum($x);
		$y_sum = array_sum($y);
		
		$xx_sum = 0;
		$xy_sum = 0;
		
		for($i = 0; $i < $n; $i++)
		{
			$xy_sum+=($x[$i]*$y[$i]);
			$xx_sum+=($x[$i]*$x[$i]);
		}
		
		// calculate slope
		$m = (($n * $xy_sum) - ($x_sum * $y_sum)) / (($n * $xx_sum) - ($x_sum * $x_sum));
		
		// calculate intercept
		$b = ($y_sum - ($m * $x_sum)) / $n;
		
		// return result
		return array("m"=>$m, "b"=>$b);
	
	}
	
	
	/**
	 * 
	 * @param $array
	 * @return array
	 */
	public static function sortedKeys($array)
	{
		$keys = array_keys($array);
		sort($keys);
		return $keys;
	}

 }
